// pages/personal/agency-list.js
const app = getApp()
const pool = new Map()
Page({

    data: {
        tabs: [{
                label: "已报价",
                status: 11
            },
            {
                label: "进行中",
                status: 12
            },
            {
                label: "已完成",
                status: 13
            },
        ],
        statusObj: {
            1: "待中标", //完成报价
            2: "未中标",
            3: "待处理", //竞标成功
            4: "已处理", //勾选完成
            5: "已完成",
            6: "已关闭",
        },
        current: 11,
        page: 1,
        total: 2,
        loading: false,
        list: []
    },
    onLoad: function(options) {
       
    },
    onShow(){
        pool.clear()
        this.loadData()
    },
    tab() {
        if (!pool.has(this.data.current)) {
            pool.set(this.data.current, {
                page: 1,
                total: 2,
                loading: false,
                list: []
            })
        }
        return pool.get(this.data.current)
    },
    loadData() {
        let tab = this.tab()
        this.setData(tab)
        if (tab.list.length >= tab.total || tab.loading) return
        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '加载中...',
        })
        app.http.post('rest/ticket/queryAgencyList.do', {
            data: {
                page: tab.page,
                rows: tab.rows,
                status: this.data.current,
                orderString: 'createTime.desc'
            }
        }).then(res => {
            tab.page++
                tab.total = res.obj.total,
                tab.loading = false
            tab.list.push(...res.obj.rows)
            this.setData(tab)
            wx.hideLoading()
            wx.stopPullDownRefresh()
        })
    },
    clickTab(evt) {
        let current = evt.currentTarget.dataset.status
        this.setData({
            current,
        })
        this.loadData()
    },
    toDetail(evt) {
        let {
            cid,
            id,
            status
        } = evt.currentTarget.dataset
        if (status == 2) {
            return
        }
        let url = ''
        if (status == 1) {
            url = '/pages/agency/demand/offer-success'
        }
        if (status == 3 || status == 5 || status == 4 || status == 6) {
            url = '/pages/agency/demand/bidding-success'
        }
        // if (status == 4) {
        //     url = '/pages/agency/demand/bidding-success'
        // }
        // if (current == 12 && status == 4) {
        //     url = '/pages/agency/demand/check-success'
        // }

        wx.navigateTo({
            url: url + '?id=' + id + '&cid=' + cid,
        })

    },
    onPullDownRefresh: function() {
        let tab = this.tab()
        tab.page = 1
        tab.total = 2
        tab.loading = false
        tab.list = []
        this.setData(tab)
        this.loadData()
    },

    onReachBottom: function() {
        this.loadData()
    },
})